<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <div id="root">
        <ul id="myList">
            <li data-item="123">item 1</li>
            <li data-item="234">item 2</li>
            <li data-item="345">item 3</li>
            <li data-item="456">item 4</li>
        </ul>
        <div id="container" data-item="ab1">hello</div>
        <button id="btn">添加节点</button>
    </div>

    <script>
        // // 事件委托
        // document.getElementById('root').addEventListener('click', function (event) {
        //     console.log(event.target.tagName.toLowerCase()) // 打印标签名
        //     console.log(event.target.dataset.item) // 打印自定义数据属性

        //     if(event.target.dataset.item === 'ab1'){
        //         console.log('hello is clicked')
        //     }
        //     if(event.target.tagName.toLowerCase() === 'li'){
        //         console.log('list is clicked')
        //     }
        // })
        document.getElementById('myList').addEventListener('click', function (event) {
            console.log(event.target.innerText)
        })
        document.getElementById('btn').addEventListener('click', function (event) {
            // 动态添加到节点
            const newLi = document.createElement('li')
            newLi.innerText = 'new item'
            document.getElementById('myList').appendChild(newLi)
            // 给新的li 添加事件
            newLi.addEventListener('click', function (event) {
                console.log('new li is clicked')
            })
        })
    </script>
</body>

</html>